﻿using System;
using FlowCanvas;
using FlowCanvas.Nodes;
using ParadoxNotion.Design;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")]
    public class TriggerGuideAction : CallableActionNodeBase
    {
        [HideInInspector] public FlowOutput onComplete;

        private int curActionType;
        private string curActionParams;
        
        public static Action<int,string> OnActionComplete;
        
        private void Invoke(int actionType, string actionParams)
        {
            BlueprintUtils.TriggerGuideAction(actionType,actionParams);

            OnActionComplete -= ActionComplete;
            OnActionComplete += ActionComplete;
        }

        private void ActionComplete(int aType,string aParams)
        {
            if (curActionType == aType && curActionParams == aParams)
            {
                onComplete?.Call(new Flow());
            }
        }

        protected override void OnRegisterPorts(FlowNode node)
        {
            var p1 = node.AddValueInput<int>("actionType");
            var p2 = node.AddValueInput<string>("actionParams");
            
            var o = node.AddFlowOutput("Out");

            node.AddFlowInput("In", (f) =>
            {
                curActionType = p1.value;
                curActionParams = p2.value;
                Invoke(p1.value,p2.value);
                o.Call(f);
            });
            onComplete = node.AddFlowOutput("onComplete");
        }
        
        public override void OnGraphStoped()
        {
            base.OnGraphStoped();
            OnActionComplete -= ActionComplete;
        }
    }
}